<template>
  <div class="list">
    <el-row>
      <el-col :span="24">
        <el-button type="primary" @click="$router.push({path:'/platform/add'})">添加</el-button>
      </el-col>
    </el-row>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID">
      </el-table-column>
      <el-table-column prop="name" label="平台名称">
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="{row,index}">
          <el-switch :value="row.status === 0" active-color="#13ce66" inactive-color="#ff4949"
            @change="editStatus(row)">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <!--  <el-button @click="handleClick(scope.row)" type="text" >查看</el-button> -->
          <el-button size="mini" @click="toEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="toDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background layout="prev, pager, next" :total="total" :page-size="size" @prev-click="prev()"
      @next-click="next()" @current-change="currentchange" style="margin-top: 20px;">
    </el-pagination>
  </div>


</template>

<script>
  import {
    ajaxPost
  } from "@/api/ajax"
  export default {
    name: "list",
    data() {
      return {
        tableData: [],
        page: 1,
        size: 15,
        total: 0,
        current_page: 1,
      };
    },
    created() {
      this.getList();
    },
    methods: {
      getList() {
        let data = {
          page: this.page,
          size: this.size,
        };
        ajaxPost('platform/list', data).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.list;
            this.total = res.data.total;
            this.current_page = res.data.current_page;
          }
        })
      },
      prev() {
        this.page -= 1;
        this.getList();
      },
      next() {
        this.page += 1;
        this.getList();
      },
      currentchange(value) {
        this.page = value;
        this.getList();
      },
      editStatus(val) {
        ajaxPost("platform/editStatus", val).then(res => {
          if (res.code == 0) {
            this.getList();
          }
        })
      },
      toEdit(row) {
        this.$router.push({
          name: 'platform_edit',
          params: row
        });
      },
      toDel(id) {
        this.$confirm('确认删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          ajaxPost('platform/delete', {
            id: id
          }).then(res => {
            if (res.code == 0) {
              this.$message({
                type: 'success',
                message: '删除成功!',
              });
               this.getList();
            } else {
              this.$message({
                type: 'error',
                message: '删除失败!'
              });
            }
          })
         
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    },
  }
</script>

<style lang="scss">
  .list {
    padding: 20px;
    box-sizing: border-box;

    .search-input {
      width: 150px;
    }

    .list-page {
      margin-top: 20px;
    }
  }
</style>
